package com.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.petshop.mapper.PetsMapper;
import com.petshop.pojo.Pets;
import com.petshop.service.PetsService;
import com.petshop.utils.Result;
import com.petshop.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 20289
 * @description 针对表【pets(存储用户宠物信息)】的数据库操作Service实现
 * @createDate 2025-02-27 11:17:00
 */
@Service
public class PetsServiceImpl extends ServiceImpl<PetsMapper, Pets>
        implements PetsService {
    @Autowired
    private PetsMapper petsMapper;

    /**
     * 插入一条宠物信息
     *
     * @param pets
     * @return
     */
    @Override
    public Result insertPet(Pets pets) {
        LambdaQueryWrapper<Pets> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Pets::getPetId, pets.getPetId());
        Long count = petsMapper.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            //宠物信息已经存在
            return Result.error("宠物信息已经存在，请勿重复添加");
        }
        //宠物信息不存在，添加宠物信息
        petsMapper.insert(pets);
        return Result.ok(null);
    }

    /**
     * 删除一条宠物信息
     *
     * @param petId
     * @return
     */
    @Override
    public Result deletePetById(Integer petId) {
        LambdaQueryWrapper<Pets> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Pets::getPetId, petId);
        Long count = petsMapper.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            //宠物信息存在，删除
            petsMapper.deleteById(petId);
            return Result.ok(null);
        }
        return Result.error("宠物信息已删除！");
    }

    /**
     * 根据宠物id查询自己的宠物信息
     *
     * @param petId
     * @return
     */
    @Override
    public Result queryPet(Integer petId) {
        LambdaQueryWrapper<Pets> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Pets::getPetId, petId);
        List<Pets> petsList = petsMapper.selectList(lambdaQueryWrapper);
        return petsList != null ? Result.ok(petsList.get(0)) : Result.build(null, ResultCodeEnum.USERNAME_ERROR);
    }

    /**
     * 修改宠物的信息
     *
     * @param pets
     * @return
     */
    @Override
    public Result updatePet(Pets pets) {
        Integer version = petsMapper.selectById(pets.getPetId()).getVersion();
        //加上乐观锁，以免和其它人修改冲突了
        pets.setVersion(version);
        //更新修改时间
        pets.setUpdatedAt(new Date());
        petsMapper.updateById(pets);
        return Result.ok(null);
    }

    /**
     * 查询全部宠物信息
     *
     * @return
     */
    @Override
    public Result queryAllPet() {
        //无条件查询
        List<Pets> pets = petsMapper.selectList(null);
        return Result.ok(pets);
    }

    /**
     * 根据宠物名模糊查询宠物信息
     *
     * @param name
     * @return
     */
    @Override
    public Result queryPetByName(String name) {
        QueryWrapper<Pets> queryWrapper
                = new QueryWrapper<>();
        queryWrapper.like("name", name);
        Long count = petsMapper.selectCount(queryWrapper);
        //库存中没有该商品
        if (count == 0) {
            return Result.error("库存中没有该商品");
        }
        //库存存在该商品，显示所有信息
        List<Pets> list = petsMapper.selectList(queryWrapper);
        return Result.ok(list);
    }


}




